package com.dudu.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dudu.bean.UserBean;
import com.dudu.enumeration.OperateTypeEnum;
import com.dudu.service.IAppExtendService;
import com.dudu.service.ILogService;
import com.dudu.service.ISmsConfigureService;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.service.impl.LogService;
import com.dudu.service.impl.SmsConfigureServiceImpl;
import com.dudu.utils.EnumUtility;

import core.CoreService;
/**
 * 短信配置(配置应用与签 名关系,配置应用与模板关系)
 * @author gaoxs
 *
 */
public class SmsConfigureController extends CoreService implements ISmsConfigureController {
	// 当前登陆用户
	private final String KEY_CURRENT_USER = "key_current_user";
	// 用于记录相应接口调用的日志
	private ILogService logService = new LogService();
	// 应用扩展信息相关
	private IAppExtendService appExtendService = new AppExtendServiceImpl();
	//短信配置相关
	private ISmsConfigureService smsService=new SmsConfigureServiceImpl();
	
	/**
	 * 查询 按分页条件查询
	 * @return
	 */
	public String queryAll() {
		log.info("SmsConfigureController.queryAll");
		JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
        if (currentUser != null) {
        	//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = this.appExtendService.getTotalAcount( paramSql);
	        List<JSONObject> list = this.appExtendService.queryAll(paramSql, pageSql);
	        jsonArray = JSONArray.fromObject(list);
	        
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
        } else {
			object.put("flag", "100000");
		}
        return returnData(object.toString());
	}
	/**
	 * 已订购的模板
	 * @return
	 */
	public String queryTemplateApp(){
		log.info("SmsConfigureController.queryTemplateApp");
		//最后返回的对象
		JSONObject object = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
	        String app_key=this.getRequest().getParameter("app_key");
	        // 总数量
	        List<JSONObject> list = this.smsService.queryAllTemplateApp(app_key);
	        JSONObject dataobject = new JSONObject();
	        if(null==list){
	        	dataobject.put("totalItemNum", 0);
	 	        dataobject.put("items", null);
	        }else{
	        	jsonArray = JSONArray.fromObject(list);
	 	        dataobject.put("totalItemNum", list.size());
	 	        dataobject.put("items", jsonArray);
	        }
	        object.put("data", dataobject);
	        object.put("flag", "000000");
 	        object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 查询未订购的模 板
	 * @return
	 */
	public String queryUnTemplateApp(){
		log.info("SmsConfigureController.queryUnTemplateApp");
		//最后返回的对象
		JSONObject object = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        String app_key=this.getRequest().getParameter("app_key");
	        // 总数量
	        int totalAcount = this.smsService.getTotalTemplateApp(app_key, paramSql);
	        List<JSONObject> list = this.smsService.queryAllUnTemplateApp(app_key,paramSql, pageSql);
	        jsonArray = JSONArray.fromObject(list);
	        
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 新增配置模板
	 * @return
	 */
	public String insertTemplateApp(){
		log.info("SmsIpController.insertTemplateApp");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			// 转化为json对象
			JSONObject jsonobject = JSONObject.fromObject("{\"passports\":"+dataStr+"}");
			JSONArray array = jsonobject.getJSONArray("passports");
		    for (int i = 0; i < array.size(); i++) {
		    	JSONObject objectTemp = (JSONObject)array.get(i);
		    	String app_key=objectTemp.getString("app_key");
		    	String template_code=objectTemp.getString("template_code");
		    	smsService.insertTemplateApp(app_key, template_code);
		    	// 日志输出信息
				logService.log(OperateTypeEnum.DELETE.getKey(), currentUser.getId(), currentUser.getName(), 
						"t_sms_template_app","新增应用标识:"+app_key+"与模板"+template_code+"的关系");
		    }
		    object.put("flag", "000000");
		    object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 删除已配置的模板
	 * @return
	 */
	public String deleteTemplateApp(){
		log.info("SmsConfigureController.deleteTemplateApp");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			//转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			String app_key=data.getString("app_key");
			String template_code=data.getString("template_code");
			int flag=smsService.deleteTemplateByAppKey(app_key, template_code);
			if(flag!=-1){
				object.put("flag", "000000");
			    object.put("desc", "success");
			}else{
				object.put("flag", "100000");
			}
			// 日志输出信息
			logService.log(OperateTypeEnum.DELETE.getKey(), currentUser.getId(), currentUser.getName(), 
					"t_sms_template_app","删除应用标识:"+app_key+"与模板"+template_code+"的关系");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 查询已订购的签 名
	 * @return
	 */
	public String querySignCodeApp(){
		log.info("SmsConfigureController.querySignCodeApp");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			String app_key=this.getRequest().getParameter("app_key");
			object=smsService.querySignCodeApp(app_key);
			if(EnumUtility.isNotBlank(object)){
				if(object.containsKey("signcode")){
					object.put("flag", "000000");
				}else{
					object.put("flag", "100000");
					object.put("signcode", "");
				}
			}else{
				object = new JSONObject();
				object.put("flag", "100000");
				object.put("signcode", "");
			}
		}else{
			object.put("flag", "100000");
			object.put("signcode", "");
		}
		return returnData(object.toString());
	}
	/**
	 * 新增配置签 名(应用与签名一对一)
	 * @return
	 */
	public String insertSignCodeApp(){
		log.info("SmsConfigureController.insertSignCodeApp");
		//最后返回的对象
		JSONObject object = new JSONObject();
		//获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			//转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			String app_key=data.getString("app_key");
			String signcode=data.getString("signcode");
			int flag=smsService.insertSignCodeApp(app_key, signcode);
			if(flag!=-1){
				object.put("flag", "000000");
			    object.put("desc", "success");
			}else{
				object.put("flag", "100000");
			}
			// 日志输出信息
			logService.log(OperateTypeEnum.ADD.getKey(), currentUser.getId(), currentUser.getName(), 
					"t_sms_signcode_app","应用标识:"+app_key+"配置签 名为:"+signcode);
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
}
